package com.atguigu.gmall.common.service;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @author: JC
 * @date: 2021/11/10 19:05
 */
@Service
public class RabbitService {

    // 消息发送的方法
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 表示成功发送消息
     * @param exchange
     * @param routingKey
     * @param message
     * @return
     */
    public boolean sendMessage(String exchange, String  routingKey, Object message){
        // 发送消息！
        rabbitTemplate.convertAndSend(exchange,routingKey,message);
        return true;
    }

    /**
     * 发送延迟消息
     * @param exchange 交换机
     * @param routingKey 路由键
     * @param message 消息
     * @param delayTime 单位：秒
     */
    public boolean sendDelayMessage(String exchange, String routingKey, Object message, int delayTime) {

        this.rabbitTemplate.convertAndSend(exchange, routingKey, message, msg ->{
            msg.getMessageProperties().setDelay(delayTime*1000);
                return msg;
        });
        return true;
    }

}
